<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <table>
        <tr>
            <td>
                <label for="">标题：</label>
                <input type="text" placeholder="请输入标题" name="biaoti"> 
            </td>
            <td>
                <label for="">摘要：</label>
                <input type="text" placeholder="请输入摘要" name="zhaiyao"> 
            </td>
            <td>
                <label for="">内容：</label>
                <input type="text" placeholder="请输入内容" name="neirong"> 
            </td>
            <td>
                <label for="">分类：</label>
                <input type="text" placeholder="请输入分类" name="fenlei"> 
            </td>
            <td>
                <label for="">作者：</label>
                <input type="text" placeholder="请输入作者" name="zuozhe"> 
            </td>
            <td>
                <input type="button" value="保存" onclick="save()" >
                <input type="button" value="取消" onclick="cancel()"> 
            </td>
        </tr>
    </table>
    <script src="./js/getUrlParam.js"></script>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="/js/init.js"></script>
    <script src="./js/api.js"></script>
   <script>
 $(function(){
            let id = getUrParam('id');
            console.log(id);
            getProductById(id).then(res =>{
                console.log(res);
                let obj = res.data;
                $('[name=biaoti]').val(obj.biaoti);
                $('[name=zhaiyao]').val(obj.zhaiyao);
                $('[name=neirong]').val(obj.neirong);
                $('[name=fenlei]').val(obj.fenlei);
                $('[name=zuozhe]').val(obj.zuozhe);
            })
        })
        function save() {
            let id = getUrlParam('id');
            console.log(id);
            let obj = {
                biaoti:$('[name=biaoti]').val(),
                zhaiyao:$('[name=zhaiyao]').val(),
                neirong:$('[name=neirong]').val(),
                fenlei:$('[name=fenlei]').val(),
                zuozhe:$('[name=zuozhe]').val(),
            }
            if (id) {
                updateProduct(id,obj).then(res =>{
                    console.log(res);
                    location.href='./index.html';
                })
            }else{
                let params = {
                    biaoti: obj.biaoti,
                    zhaiyao: obj.zhaiyao,
                    neirong: obj.neirong,
                    fenlei: obj.fenlei,
                    zuozhe: obj.zuozhe,
                }
                addProduct(params).then(res=>{
                    location.href='./index.html';
                })
            }
        }
        function cancel() {
            location.href='./index.html';
        }
   </script>
</body>
</html>